﻿using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PcProject.Infrastructure.Repository
{
    public class OrderRepository: DapperRepositoryBase<Orders>, IOrderRepository
    {
        //构造函数依赖注入
        public IConfiguration _configuration;
        //函数依赖注入
        public OrderRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }


        /// <summary>
        /// 订单显示查询分页
        /// </summary>
        /// <param name="number">查询参数</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public List<OrderAll> GetOrdersAsync(string number, int pageIndex, int pageSize, out int totalCount)
        {
            string sql = $"select * from Orders a left join Users b on a.UserId=b.UserId left join OrderDetail c on a.OrderId=c.OrId left join Product d on c.PrIds=d.ProId where 1=1";
            //商品名称查询
            if (!string.IsNullOrEmpty(number))
            {
                sql += $" and OrderNumber like '%{number}%'";
            }
            sql += $" limit @p1,@p2";
            //计算条数
            string sqlcount = $"select count(*) from Orders a left join Users b on a.UserId=b.UserId left join OrderDetail c on a.OrderId=c.OrId left join Product d on c.PrIds=d.ProId ";
            //总条数
            totalCount = Conn.ExecuteScalar<int>(sqlcount);
            return Conn.Query<OrderAll>(sql, new { p1 = (pageIndex - 1) * pageSize, p2 = pageSize }).ToList();
        }

        /// <summary>
        /// 订单添加
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> InserAsync(Orders obj)
        {
            return (int)await Conn.InsertAsync(obj);
        }

        /// <summary>
        /// 订单删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> DeleAsync(int id)
        {
            string sql = $"delete from Orders where OrderId={id}";
            return await Conn.ExecuteAsync(sql);
        }

        /// <summary>
        /// 订单反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<OrderAll> FillAsync(int id)
        {
            return await Conn.GetAsync<OrderAll>(id);
        }

        /// <summary>
        /// 订单修改
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> UptAsync(Orders obj)
        {
            return (int)await Conn.UpdateAsync(obj);
        }

    }
}
